package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.app.domain.AppImMessageFavorite;
import com.ruoyi.app.domain.bo.AppImMessageFavoriteBo;
import com.ruoyi.app.domain.vo.AppImMessageFavoriteVo;
import com.ruoyi.app.mapper.AppImMessageFavoriteMapper;
import com.ruoyi.app.service.IAppImMessageFavoriteService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 消息收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-19
 */
@RequiredArgsConstructor
@Service
public class AppImMessageFavoriteServiceImpl implements IAppImMessageFavoriteService {

    private final AppImMessageFavoriteMapper baseMapper;

    /**
     * 查询消息收藏
     */
    @Override
    public AppImMessageFavoriteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消息收藏列表
     */
    @Override
    public TableDataInfo<AppImMessageFavoriteVo> queryPageList(AppImMessageFavoriteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppImMessageFavorite> lqw = buildQueryWrapper(bo);
        Page<AppImMessageFavoriteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (!CollUtil.isEmpty(result.getRecords())){
            SimpleDateFormat sdf = new SimpleDateFormat("M月d日 aHH:mm", Locale.CHINA);
            result.getRecords().forEach(item -> {
                item.setTime(sdf.format(item.getCreateTime()));
            });

        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询消息收藏列表
     */
    @Override
    public List<AppImMessageFavoriteVo> queryList(AppImMessageFavoriteBo bo) {
        LambdaQueryWrapper<AppImMessageFavorite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppImMessageFavorite> buildQueryWrapper(AppImMessageFavoriteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppImMessageFavorite> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AppImMessageFavorite::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getSearchValue()), AppImMessageFavorite::getKeyWord, bo.getSearchValue());
        lqw.eq(bo.getMessageId() != null, AppImMessageFavorite::getMessageId, bo.getMessageId());
        if (bo.getType()!=null){
            if (bo.getType()==6){
                lqw.eq(AppImMessageFavorite::getType, 4).or(lqw1 -> lqw1.eq(AppImMessageFavorite::getType, 5));
            }else {
                lqw.eq(AppImMessageFavorite::getType, bo.getType());
            }
        }

        lqw.like(StringUtils.isNotBlank(bo.getKeyWord()), AppImMessageFavorite::getKeyWord, bo.getKeyWord());
        return lqw;
    }

    /**
     * 新增消息收藏
     */
    @Override
    public Boolean insertByBo(AppImMessageFavoriteBo bo) {
        AppImMessageFavorite add = BeanUtil.toBean(bo, AppImMessageFavorite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消息收藏
     */
    @Override
    public Boolean updateByBo(AppImMessageFavoriteBo bo) {
        AppImMessageFavorite update = BeanUtil.toBean(bo, AppImMessageFavorite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppImMessageFavorite entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<AppImMessageFavorite> appImMessageFavoriteLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appImMessageFavoriteLambdaQueryWrapper.eq(AppImMessageFavorite::getUserId, entity.getUserId())
                .eq(AppImMessageFavorite::getMessageId, entity.getMessageId());
        List<AppImMessageFavorite> appImMessageFavorites = baseMapper.selectList(appImMessageFavoriteLambdaQueryWrapper);
        if (CollUtil.isNotEmpty(appImMessageFavorites)){
            throw new RuntimeException("消息已收藏");
        }
    }

    /**
     * 批量删除消息收藏
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        baseMapper.deleteBatchIds(ids);
        return true;
    }
}
